import { http } from '@/http/http'

// 定义通用响应类型
interface ApiResponse<T = any> {
  code: number
  msg: string
  data: T
  success?: boolean
  status?: number
  result?: T
}
// 地区数据结构
export interface AreaItem {
  code: string
  name: string
  parentCode: string
  shortName: string
  levelType: number
  latitude: number
  longitude: number
  mergerName: string
  pinyin: string
  pinyinPrefix: string
  children?: AreaItem[]
}

// 用户信息编辑接口
export interface InfoEdit {
  cityCode: string
  districtCode: string
  headimgurl: string
  nickname: string
  provinceCode: string
  sex: string
  [property: string]: any
}

// 用户相关接口
const apiUser = {
  // 获取验证码
  sendcode: (params: { mobile: string, codeType?: number }) =>
    http.post<ApiResponse>('/sys/sms/send', params),

  // 获取换绑绑定手机号验证码
  sendcodeBind: (params: { mobile: string, codeType?: number }) =>
    http.post<ApiResponse>('/app/auth/sys/sms/send', params),
  // 换绑手机号
  bindnewMobile: (params: { mobile: string, smsCode: string }) =>
    http.post<ApiResponse>('/app/auth/users/mobile/rest', params),
  // 手机号绑定
  bindMobile: (params: { mobile: string, smsCode: string }) =>
    http.post<ApiResponse>('/app/auth/users/mobile/bind', params),
  // 微信登录
  tokenSocial: (params: { js_code: string, platform: number }) =>
    http.get<ApiResponse>('/app/users/weixin/mini/jscode2session', params),

  // 手机号登录
  login: (params: { code: string }) =>
    http.get<ApiResponse>(`/app/users/weixin/mini/login`, params),

  // 短信验证码登录
  mobileLogin: (params: { mobile: string, smsCode: string, clientInfoType?: string, fromInviteCode?: string, roleType?: string }) =>
    http.post<ApiResponse>('/app/users/mobile/login', params),

  // 获取用户令牌
  tokenSocial2: (params: { code: string, wxId: string, userRole?: number, inviterId?: string }) =>
    http.post<ApiResponse>('/app/users/weixin/mini/mobile', params),

  // 注册
  register: (params: Record<string, any>) =>
    http.post<ApiResponse>('/users/client/register', params),

  // 注销账户
  usersCancel: (params?: Record<string, any>) =>
    http.get<ApiResponse>('/app/auth/users/cancel', params),

  // 获取openid
  getOpenid: (params?: Record<string, any>) =>
    http.get<ApiResponse>('/app/users/weixin/mini/openid', params),

  // 系统全局配置
  getDictInfo: (params?: Record<string, any>) =>
    http.get<ApiResponse>('/app/sys/dict/info', params),

  // 获取用户信息
  getUserInfo: () =>
    http.get<ApiResponse>('/app/auth/users/info'),

  // 修改用户信息
  changeUserInfo: (params: Record<string, any>) =>
    http.post<ApiResponse>('/app/auth/users/upd/info', params),

  // 个人中心
  getUserPersonal: (params?: Record<string, any>) =>
    http.get<ApiResponse>('/app/users/personal', params),

  // 合作咨询-已登录
  getCooperateApply: (params?: Record<string, any>) =>
    http.get<ApiResponse>('/app/auth/users/cooperate/apply', params),
  // 合作咨询-未登录
  getCooperateApplyUnlogin: (params?: Record<string, any>) =>
    http.post<ApiResponse>('/app/users/cooperate/apply', params),

  // 获取地区列表(带层级)
  getAreaList: (params?: Record<string, any>) =>
    http.get<ApiResponse<AreaItem[]>>('/sys/area/list', params),

  // 获取地区子列表(分级查询)
  getAreaChildList: (params: { parentCode: string }) =>
    http.get<ApiResponse<AreaItem[]>>('/sys/area/list/child', params),

  // 更新用户信息
  updateUserInfo: (params: InfoEdit) =>
    http.post<ApiResponse>('/app/auth/users/upd/info', params),

  // 创建上传
  createUpload: (params: Record<string, any>) =>
    http.get<ApiResponse>('/app/auth/system/sts/policy', params),

  // IM访客登录
  customerGuest: (params: Record<string, any>) =>
    http.post<ApiResponse>('/app/users/workbench/customer/login', params),

  // IM发起会话
  getNetworkStart: (params: Record<string, any>) =>
    http.post<ApiResponse>('/app/users/workbench/contact/network/start', params),

  // 关闭会话
  closeConversation: (params: Record<string, any>) =>
    http.post<ApiResponse>('/app/users/workbench/contact/network/end', params),
}

export default apiUser
